<template>
	<view class="show-form"   v-if="itemList.length > 0"> 
		<view class="show-form-list" v-for="(item,index) in itemList" :key="index">
			<view class="list-title">
				<view class="number">{{index+1}}.</view>
				<view class="input-title"><input type="text" v-model="item.value"   placeholder="点击此处设置标题" placeholder-class="place-class" class="input-in"></view>
				<view class="title-tip">{{item.right_title_tip?item.right_title_tip:item.title}}</view>
			</view>
			
			<view v-if="item.type == 'radio' || item.type == 'checkbox'">
				<view class="radio-0" v-for="(c_item,c_index) in item.ext" :key="c_index" v-if="item.ext">
					<view class="radio-0-1">
						<view class="radio-0-1-number">{{c_index+1}}</view>
					</view>
					<view class="radio-0-2">
						<input type="text" :placeholder="'选项'+(c_index+1)" v-model="item.ext[c_index]" 
						placeholder-class="place-class" class="input-in">
					</view>
					<view class="radio-0-3">
						<text class="iconfont icon-shanchu-mian" @tap.stop="showAddCheck('del',index,c_index)"></text>
					</view>
				</view>
				<view class="add-select">
					<view class="add-select-one" @tap="showAddCheck('addone',index)">添加选项</view>
					<view class="add-select-shuxian"></view>
					<view class="add-select-one" @tap="showAddCheck('addmroe',index)">批量加</view>
				</view>
				
			</view>
			<view class="ope">
				<view class="ope-list">
					<view class="ope-list-1" @tap.stop="mustOrShowStatus(index,'must_status')">
						<text class="iconfont icon-xuanzhong"  v-if="item.must_status == 1"></text>
						<text class="iconfont icon-weixuanzhong" v-else></text>
						<text class="text">必填</text>
					</view>
					<view class="ope-list-2" @tap.stop="mustOrShowStatus(index,'show_status')">
						<text class="iconfont icon-xuanzhong"  v-if="item.show_status == 1"></text>
						<text class="iconfont icon-weixuanzhong"  v-else></text>
						<text class="text">公开</text>
					</view>
				</view>
				<view class="ope-list-right">
					<view class="right-sel" v-if="item.type == 'username'" @tap.stop="showAddUser('show',index)">设置人员</view>
					<view class="right-sel" v-if="item.type == 'image'" @tap.stop="showImageSel(index)">切换模式</view>
					<view class="right-sel" @tap="delItem(index)">删除</view>
					<view class="right-sel" v-if="index < (itemList.length -1)" @tap="moveType(index,'down')">下移</view>
					<view class="right-sel" v-if="index > 0" @tap="moveType(index,'up')">上移</view>
				</view>
			</view>
		</view>
		
		<!-- 点击批量加的时候显示 -->
		<view class="add-check_s" v-if="showAddCheckStatus == true">
			
			<view class="add-check-title" :style="{'margin-top':(addCheckTop+'rpx')}">批量添加设置项，一行一个</view>
			<textarea class="add_text_area" placeholder-class="place-class" maxlength="-1" fixed="false"
			 placeholder="请输入选项,一行一个"  v-model="addCheckList"/>
			 <view class="add-check-button">
				 <view class="add-check-cls add-check-cancel fl" @tap="showAddCheck('cancel')">取  消</view>
				 <view class="add-check-cls add-check-confirm fl" @tap="showAddCheck('confirm')">确  认</view>
			 </view>
		</view>
		<!-- 点击批量加的时候显示 -->
		<view class="add-check_s" v-if="showAddUserStatus == true">
			
			<view class="add-check-title" :style="{'margin-top':(addCheckTop+'rpx')}">设置人员，一行一个<text class="textred" v-if="addUserCount > 0">({{addUserCount}}人)</text></view>
			<textarea class="add_text_area" placeholder-class="place-class" maxlength="-1" fixed="false"
			 placeholder="请输入人员姓名,一行一个"  v-model="addUserList" @blur="confirmUserName"/>
			 <view class="username-set flex align-center " >
				 <view class="mingdan-btn flex align-center" v-for="(item,index) in userNameButtonList" :key="index">
					 <view class="btn-list" @tap.stop="userBtnOpe(index)">{{item}}</view>
				 </view>
			 </view>
			 <view class="username-set flex align-center"  v-if="userNameradioListId > 0">
				 <view @tap="showTips('username-set')" class="tc-title">填充模式<text class="iconfont icon-wenhao"></text>：</view>
				 <view class="username-radio">
					 <radio-group @change="userNameradioChange" class="flex align-center">
						<label class="username-radio-label flex align-center" v-for="(item, index) in userNameradioList" :key="index">
							<view>
								<radio class="username-radio-c" :value="item.id" :checked="userNameradioListId == item.id" />
							</view>
							<view>{{item.title}}</view>
						</label>
					</radio-group>
				 </view>
			 </view>
			 <view class="add-check-button">
				 <view class="add-check-cls add-check-cancel fl" @tap="showAddUser('cancel')">取  消</view>
				 <view class="add-check-cls add-check-confirm fl" @tap="showAddUser('confirm')">确  认</view>
			 </view>
		</view>
	</view>
</template>

<script>
	export default {
		name:'show-form',
		data() {
			return {
				userNameButtonList:['清空名单','粘贴名单','保存名单','预存名单'],
				userNameradioList:[
					{id:'1',title:'选择式'},
					{id:'2',title:'填写式'},
				],
				userNameradioListId:0,
				//多选单选控制参数
				showAddCheckStatus:false,
				addCheckList:'',
				addCheckListIndex:-1,
				//人员设置控制参数
				showAddUserStatus:false,
				addUserList:'',
				addUserCount:0,
				addUserListIndex:-1,
				//itemList:[],
			}
		},
		props:{
			itemList:{
				type:Array,
				default:[]
			},
			addCheckTop:{
				type: [Number, String],
				default:30
			},
		},
		watch:{
			itemList(a,b){
				
			}
		},
		methods: {
			parentSetUsername(){
				var username = uni.getStorageSync('userlist-info-content');
				if(this.addUserListIndex != -1)
				{
					if(username && username.length > 0)
					{
						this.addUserList = username.join('\n');
						this.confirmUserName();
					}
				}
				uni.removeStorageSync('userlist-info-content');
			},
			userBtnOpe(index){
				var that = this;
				switch(index){
					case 0:
						if(that.addUserList && that.addUserList != '')
						{
							that.$api.showModal('','确认清空以上设置的名单吗',function(res){
								if(res == 'yes')
								{
									that.addUserList = '';
									that.addUserCount = 0;
								}
							})
						}else{
							that.addUserCount = 0;
						}
						break;
					case 1:
						uni.getClipboardData({
							success: function (res) {
								if(res.data && res.data.trim() != '')
								{
									if(that.addUserList && that.addUserList != '')
									{
										that.$api.showModal('','检测到当前您已设置名单，是否覆盖现有的内容',function(check){
											if(check == 'yes')
											{
												that.addUserList = res.data;
												that.confirmUserName();
											}
										})
									}else{
										that.addUserList = res.data;
										that.confirmUserName();
									}
								}else{
									that.$api.msg('未获取到要粘贴的内容');
								}
								
							}
						});
						break;
					case 2:
						var checkLogin = that.$api.checkLogin(false);
						if(checkLogin == false)
						{
							that.$api.navigateTo('/pages/member/login');
							return ;
						}
						var arr = this.$api.splitStrRN(this.addUserList,true);
						if(arr.status == true && arr.list.length >0)
						{
							that.$api.showModal('','确认保存以上设置的名单吗',function(res){
								if(res == 'yes')
								{
									that.$api.loadingMsg('');
									that.$http.post('user/createUserName',{
										username:arr.list
									}).then(res=>{
										that.$api.msg(res.msg);
									})
								}
							})
						}else{
							that.$api.msg('未获取到人员名单');
						}
						break;
					case 3:
						that.$api.navigateTo('/pages/member/userlist/index?selType=1');
						break;
						
				}
			},
			showTips(type){
				var content = '';
				switch(type){
					case 'username-set':
						content = '仅设置人员名单时有效！“选择式”表示用户在填写时可以直接选择所设置的人员名单。“填写式”表示为保护用户隐私需手动填写，然后由系统验证填写内容是否存在于所设名单中'; 
						break;
				}
				if(content)
				{
					this.$api.showModal('',content,'',{
						hideCancel:true
					})
				}
			},
			userNameradioChange (e){
				this.userNameradioListId = e.detail.value;
			},
			//图片设置项
			showImageSel(index){
				var _this = this;
				var itemList = _this.itemList;
				uni.showActionSheet({
					itemList: ['单图模式', '多图模式'],
					success: function (res) {
						if(res.tapIndex == 0)
						{
							itemList[index].ext.type = 1;
							itemList[index].right_title_tip = '单图模式';
						}else if(res.tapIndex == 1){
							itemList[index].ext.type = 2;
							itemList[index].right_title_tip = '多图模式';
						}
						_this.$emit('change',itemList)
					},
					fail: function (res) {
						
					}
				});
			},
			//单选多选 选项添加一个
			showAddCheck(type,index,c_index){
				var itemList = this.itemList;
				if(type == 'del')
				{
					if(itemList[index].ext.length < 2)
					{
						this.$api.msg('至少保留一个选项');
						return ;
					}
					itemList[index].ext.splice(c_index,1);
				}else if(type == 'addone')
				{
					itemList[index].ext.push('');
				}else if(type == 'addmroe')
				{
					this.showAddCheckStatus = true;
					this.addCheckListIndex = index;
					var addCheckList = itemList[this.addCheckListIndex].ext;
					if(addCheckList.length > 0)
					{
						this.addCheckList = addCheckList.join('\n');
					}else{
						this.addCheckList = '';
					}
					return ;
				}else if(type =='cancel')
				{
					this.showAddCheckStatus = false;
					return ;
				}else if(type =='confirm')
				{
					var arr = this.$api.splitStrRN(this.addCheckList,false);
					if(arr.status == true)
					{
						if(arr.list.length > 0)
						{
							
							if(this.addCheckListIndex == -1)
							{
								this.$api.msg('设置失败');
								return false;
							}
						}else{
							arr.list = [''];
						}
						itemList[this.addCheckListIndex].ext = arr.list;
						this.showAddCheckStatus = false;
					}else{
						return ;
					}
				}
				this.$emit('change',itemList)
			},
			//设置人员
			showAddUser(type,index){
				var itemList = this.itemList;
				var that = this;
				if(type == 'show')
				{
					this.showAddUserStatus = true;
					this.addUserListIndex = index;
					var info = itemList[this.addUserListIndex];
					var userListArr = info.ext?info.ext:[];
					this.userNameradioListId = 0;
					setTimeout(function(){
						that.userNameradioListId = info.ext_sel_type?info.ext_sel_type:1;
					},100)
					if(userListArr.length > 0)
					{
						//var userListArr = userList.split(',');
						this.addUserList = userListArr.join('\n');
					}else{
						//var userListArr = [];
						this.addUserList = '';
					}
					this.addUserCount = userListArr.length;
					return ;
				}else if(type =='cancel')
				{
					this.showAddUserStatus = false;
					this.addUserListIndex = -1;
					this.addUserCount = 0;
					return ;
				}else if(type =='confirm')
				{
					var res = this.confirmUserName();
					if(res === false)
					{
						return ;
					}else{
						this.showAddUserStatus = false;
						//itemList[this.addUserListIndex].right_title_tip = '统计人员姓名';
					}
				}
				this.$emit('change',itemList)
			},
			confirmUserName(){
				var that = this;
				var itemList = this.itemList;
				var arr = this.$api.splitStrRN(this.addUserList,true);
				if(arr.status == true)
				{
					if(arr.list.length > 0)
					{
						//this.addUserCount = arr.list.length;
						//var arrStr = arr.list.join(',');
						if(this.addUserListIndex == -1)
						{
							this.$api.msg('设置人员失败');
							return false;
						}
						itemList[this.addUserListIndex].right_title_tip = '已设置'+arr.list.length+'人';
						/* let str = arr.list.join('\n');
						that.addUserList = 1;//
						that.$forceUpdate() 
						console.log('str',str)*/
						this.addUserList = arr.list.join('\n');
					}else{
						itemList[this.addUserListIndex].right_title_tip = '统计人员姓名';
					}
					itemList[this.addUserListIndex].ext = arr.list;
					itemList[this.addUserListIndex].ext_sel_type = (arr.list.length > 0)?this.userNameradioListId:1;
					this.addUserCount = arr.list.length;
					
					return itemList;
				}else{
					return false;
					//itemList[this.addUserListIndex].right_title_tip = '统计人员姓名';
				}
			},
			//上移下移
			moveType(index,type){
				var itemList = this.itemList;
				//down下移 up上移
				if(type == 'down')
				{
					var nowInfo = itemList[index];
					var nextInfo = itemList[index+1]?itemList[index+1]:{};
					if(!nextInfo){
						this.$api.msg('已经是最后一个了');return false
					}
					itemList[index] = nextInfo;
					itemList[index+1] = nowInfo;
				}else if(type == 'up')
				{
					var nowInfo = itemList[index];
					var upInfo = itemList[index-1]?itemList[index-1]:{};
					if(!upInfo){
						this.$api.msg('已经是第一个了');return false
					}
					itemList[index] = upInfo;
					itemList[index-1] = nowInfo;
				}
				this.$emit('change',itemList)
			},
			//删除某个元素
			delItem(index){
				var _this = this;
				uni.showModal({
					content:'确认删除',
					success(res) {
						if(res.confirm == true)
						{
							var itemList = _this.itemList;
							var newItemList = [];
							for(var i=0;i<itemList.length;i++)
							{
								if(index != i)
								{
									newItemList.push(itemList[i]);
								}
							}
							_this.$emit('change',newItemList)
						}
					}
				})
			},
			//显示填写或者公开
			mustOrShowStatus(index,type){
				let info = this.itemList[index];
				var saveStatus = info[type] == 1?-1:1;
				this.itemList[index][type] = saveStatus;
				this.$emit('change',this.itemList);//修改item的值
			},
			changeItemlist(){
				this.$emit('change',this.itemList)
			},
			//获取item最后一次更新的值
			getItemList(){
				var itemListJSON = JSON.stringify(this.itemList); //这里转化一下json 再转回来 不然数据双向绑定会同步修改
				return JSON.parse(itemListJSON);
			},
			getItemListText(){
				var textArr = [];
				var list = this.getItemList();
				if(list.length > 0)
				{
					for(var i=0;i<list.length;i++)
					{
						var strArr = [];
						var info = list[i];
						if(info.value)
						{
							strArr.push(info.value);
						}
						if(info.type == 'username' || info.type == 'radio' || info.type == 'checkbox')
						{
							if(info.ext && info.ext.length > 0)
							{
								strArr.push(info.ext.join(','));;
							}
						}
						var str = (strArr.length > 0)?strArr.join(','):'';
						if(str)
						{
							textArr.push(str);
						}
					}
				}
				var endStr = '';
				if(textArr.length > 0)
				{
					endStr = textArr.join(',');
				}
				return endStr;
			},
			
		},
		mounted(){
			
		},
	}
</script>

<style lang="scss">
	.username-set{
		padding: 20rpx;
		.tc-title{
			padding-left: 20rpx;
		}
		.mingdan-btn{
			width: 25%;
			justify-content: center;
			.btn-list{
				width: 80%;
				background: #26C390;
				color: #ffffff;
				text-align: center;
				height: 40rpx;
				font-size: 22rpx;
				line-height: 40rpx;
				border-radius: 10rpx;
			}
		}
		.iconfont{
			margin-left: 10rpx;
		}
		.username-radio{
			.username-radio-label{
				margin-left: 20rpx;
				.username-radio-c{
					transform:scale(0.7)
				}
			}
		}
	}
	
	.show-form{
		width: 100%;
		padding: 20rpx;
		.textred{
			color: red;
			font-size: 28rpx;
		}
		.add-check_s{
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background: #FFFFFF;
			z-index: 101;
			.add-check-title{
				height: 50rpx;
				line-height: 50rpx;
				padding-left: 30rpx;
				margin-top: 30rpx;
			}
			.add_text_area{
				background: #F8F8FA;
				width: 94%;
				padding: 20rpx;
				margin: 20rpx 3%;
				border-radius: 20rpx;
				height: 600rpx;
			}
			.add-check-button{
				position: absolute;
				left: 0;
				bottom: 100rpx;
				width: 100%;
				.add-check-cls{
					width: 220rpx;
					text-align: center;
					height: 70rpx;
					border-radius: 26rpx;
					line-height: 71rpx;
				}
				.add-check-cancel{
					border: 2rpx solid #8B988E;
					color: #8B988E;
					margin-left: 140rpx;
					margin-right: 50rpx;
				}
				.add-check-confirm{
					background-color: #61B080;
					color: #FFFFFF;
				}
			}
		}
		.place-class{
			font-size: 26rpx;
			color: #C3C3C5;
		}
		.add-select{
			height: 80rpx;
			display: flex;
			align-items: center;
			border-bottom: 2rpx solid #EAEAEC;
			position: relative;
			.add-select-one{
				width: 150rpx;
				font-size: 24rpx;
				color: #728DB5;
				    height: 100%;
				    line-height: 80rpx;
			}
			.add-select-shuxian{
				position: absolute;
				width: 2rpx;
				height: 20rpx;
				background-color: #C6C6C8;
				top: 33rpx;
				left: 124rpx;
			}
		}
		.show-form-list{
			background-color: #F8F8FA;
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			padding: 10rpx 16rpx;
			.radio-0{
				display: flex;
				align-items: center;
				height: 80rpx;
				.radio-0-1{
					width: 50rpx;
					.radio-0-1-number{
						width: 40rpx;
						height: 40rpx;
						line-height: 40rpx;
						border-radius: 40rpx;
						background: #EAEAEC;
						color: #8A8A8C;
						text-align: center;
						font-size: 24rpx;
						overflow: hidden;
					}
				}
				.radio-0-2{
					border-bottom: 2rpx solid #EAEAEC;
					width: 430rpx;
					margin-left: 20rpx;
					height: 100%;
					.input-in{
						height: 100%;
					}
				}
				.radio-0-3{
					height: 100%;
					border-bottom: 2rpx solid #EAEAEC;
					display: flex;
					align-items: center;
					.iconfont{
						color: #78797D;
						font-size: 40rpx;
						margin-left: 40px;
					}
				}
			}
			.ope{
				display: flex;
				align-items: center;
				height: 90rpx;
				.ope-list{
					width: 45%;
					display: flex;
					.text{
						margin-left: 8rpx;
						color: #8e8e92;
						font-size: 24rpx;
					}
					.ope-list-1{
						width: 42%;
					}
					.ope-list-2{
						width: 58%;
					}
					.iconfont{
						color: #1b1b1d;
						font-size: 26rpx;
					}
				}
				.ope-list-right{
					width: 55%;
					//display: flex;
					.right-sel{
						min-width: 60rpx;
						border: 2rpx solid #ADADAF;
						text-align: center;
						color: #79797A;
						font-size: 22rpx;
						margin-right: 6rpx;
						height: 40rpx;
						line-height: 40rpx;
						border-radius: 10rpx;
						float: right;
						padding: 0 10rpx;
					}
				}
			}
			.list-title{
				border-bottom: 2rpx solid #EAEAEC;
				display: flex;
				align-items: center;
				height: 80rpx;
				.number{
					width: 50rpx;
					overflow: hidden;
				}
				.input-title{
					width: 360rpx;
					margin-right: 24rpx;
					.input-in{
						height: 80rpx;
					}
				}
				.title-tip{
					font-size: 24rpx;
					color: #C3C3C5;
					width: 230rpx;
					text-align: right;
				}
			}
		}
	}
</style>
